// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   GridImageAdapter.java

package test.sgone.cn.mulimagedemo;

import android.content.Context;
import android.util.Log;
import android.view.*;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.ArrayList;


public class GridImageAdapter extends BaseAdapter
{

    private ArrayList dataList;
    private int height;
    private Context mContext;
    private int width;
    private int maxPic;
    private boolean reachMax = false;
    public static final String DEFAULT_IMG = "camera_default";
    private int count;
    static class ViewHolder
    {
        public ImageView imageView;
    }


    public GridImageAdapter(Context context, int maxPic)
    {
        mContext = context;
        width = -11 + DensityUtil.getScreenWidth() / 4;
        height = width;
     //   dataList = arraylist;
        if(dataList==null)
            dataList = new ArrayList();
        if(dataList != null && dataList.size()<maxPic && !dataList.contains(DEFAULT_IMG))
            dataList.add(DEFAULT_IMG);
        this.maxPic = maxPic;
    }
    public boolean isReachMax() {
        return reachMax;
    }
    public void setMaxPic(int maxPic) {
        this.maxPic = maxPic;
    }

    public int getMaxPic() {
        return maxPic;
    }
    public int getCount()
    {
        return dataList.size();
    }

    public Object getItem(int i)
    {
        return dataList.get(i);
    }

    public long getItemId(int i)
    {
        return i;
    }

    public View getView(int position, View view, ViewGroup viewgroup)
    {
        ViewHolder viewholder;
        String filePath;
        if(view == null)
        {
            viewholder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.select_image_brower_view, viewgroup, false);
            view.setLayoutParams(new android.widget.AbsListView.LayoutParams(width, height));
            viewholder.imageView = (ImageView)view.findViewById(R.id.select_image_brower_view);
            view.setTag(viewholder);
        } else
        {
            viewholder = (ViewHolder)view.getTag();
        }

        if(dataList != null && dataList.size() > position)//*/
            filePath = (String)dataList.get(position);
        else
            filePath = DEFAULT_IMG;

         Log.e("getVIew===path===",filePath+"size==="+dataList.size()+"position==="+position+"viewgroup.getChildCount()=="+viewgroup.getChildCount());
        if(filePath.contains(DEFAULT_IMG)){
            viewholder.imageView.setImageResource(R.drawable.add_pic_nor);
        }
        else if(viewgroup.getChildCount()==position){
            ImageManager2.from(mContext).displayImage(viewholder.imageView, filePath, R.drawable.today_default_pic, width, height);
        }

        return view;
    }

    public void setData(ArrayList arraylist)
    {
        dataList = arraylist;
        notifyDataSetChanged();
    }

    public ArrayList<String> getData()
    {
        return dataList;
    }
    public void addPic(String pic)
    {
        int idx = getCount() -1;
        if(idx < 0)
        {
            idx = 0;
        }

        if(shouldReplaceLastItem())
        {
            dataList.remove(idx);
            reachMax = true;
        }
        dataList.add(idx, pic); //当原有索引有对应的值，则原有的值会移动到下个索引；如果list.get(3)==3 如果list.get(3)=4，则list.get(4)=3
        Log.e("addPic===",dataList.size()+"；idx==="+dataList.get(idx));
        notifyDataSetChanged();
    }

    private boolean shouldReplaceLastItem()
    {
        return getCount() == this.maxPic;
    }

    public void removePic(int idx)
    {
        dataList.remove(idx);
        if(idx+1 == this.maxPic)
        {
            dataList.add(idx,DEFAULT_IMG);
            reachMax = false;
        }
        else if((getCount()+1 == this.maxPic) && isReachMax())
        {
            dataList.add(getCount(),DEFAULT_IMG);
            reachMax = false;
        }
        notifyDataSetChanged();
    }


}
